# ChainHandler.gd 类脚本（连击奖励计算）
class_name ChainHandler

# 成员变量声明
var current_chain_type: CardData.Type = CardData.Type.ENVIRONMENT

var current_chain := 0:
	set(value):
		current_chain = clamp(value, 0, 10)
		_EventBus.card_chain_updated.emit(current_chain)
		
# 扩展连锁效果触发逻辑
static func trigger_chain_effects(chain_count: int, card_type: CardData.Type) -> void:
	var effect = _get_chain_effect(chain_count)
	if effect:
		match card_type:
			CardData.Type.ENVIRONMENT:
				_EventBus.recovery_rate_boosted.emit(effect.multiplier, effect.duration)
			CardData.Type.ENEMY:
				_EventBus.stamina_cost_reduced.emit(1.0 / effect.multiplier, effect.duration)
				
# 连锁奖励配置
static func _get_chain_effect(count: int) -> Dictionary:
	return {
		3: {"multiplier": 1.2, "duration": 3},
		5: {"multiplier": 1.5, "duration": 5},
		8: {"multiplier": 2.0, "duration": 8}
	}.get(count, {})
					
static func calculate_chain_bonus(chain : int ,chain_type: CardData.Type) -> void:
	var bonus_map := {
		3: { "multiplier": 1.2, "duration": 3 },
		5: { "multiplier": 1.5, "duration": 5 },
		8: { "multiplier": 2.0, "duration": 8 }
	}
	if chain in bonus_map:
		var effect = bonus_map[chain]
		_apply_temporary_boost(chain_type, effect)

static func _apply_temporary_boost(type: CardData.Type, effect: Dictionary) -> void:
	match type:
		CardData.Type.ENVIRONMENT:
			_AttributeSystem.temporary_boost(
				"recovery_rate", 
				effect.multiplier, 
				effect.duration
			)
		CardData.Type.ENEMY:
			_GameManager.add_temporary_buff(
				"stamina_cost_reduction", 
				1.0 / effect.multiplier, 
				effect.duration
			)
# 连击计数器更新
func update_chain_counter(type: CardData.Type) -> void:
	if type == current_chain_type:
		current_chain += 1
	else:
		current_chain = 1
		current_chain_type = type
	
	if current_chain >= 3:
		_apply_chain_bonus()
# 应用连锁奖励
func _apply_chain_bonus() -> void:
	var bonus : float = current_chain / 3
	match current_chain_type:
		CardData.Type.ENVIRONMENT:
			_AttributeSystem.base_recovery_rate += bonus * 0.2
		CardData.Type.ENEMY:
			_GameManager.stamina += bonus * 5
